﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace SkladLib
{
    /// <summary>
    /// Interaction logic for userInterfacePolozka.xaml
    /// </summary>
    public partial class userInterfacePolozka : UserControl
    {
        public delegate void ValueChangedEventHandler(object sender);
        public event ValueChangedEventHandler AddedRow;

        public userInterfacePolozka()
        {
            InitializeComponent();
        }

        private void btnPridat_Click(object sender, RoutedEventArgs e)
        {
            if (isInputsOK())
            {
                Dictionary<string, string> row = GetData();
                DataLib.Settings.databases.dbSklad.AddRow("sklad", row);
                clearInputs();
                if (AddedRow != null)
                    AddedRow(this);
            }
        }

        private void clearInputs()
        {
            txtKod.Clear();
            txtNazev.Clear();
            Keyboard.Focus(txtKod);
        }

        private Dictionary<string, string> GetData()
        {
            Dictionary<string, string> rowVybaveni = new Dictionary<string, string>();
            rowVybaveni.Add("Name", txtNazev.Text);
            string cenovyProfilName = GetCenovyProfilName();
            rowVybaveni.Add("CenovyProfilName", cenovyProfilName);
            rowVybaveni.Add("cenovyProfilPrefix", DataLib.Settings.databases.dbCeny.QueryGetString("SELECT prefix FROM Ceny WHERE Name='" + cenovyProfilName + "'"));
            rowVybaveni.Add("naSklade", "True");
            rowVybaveni.Add("kod", txtKod.Text);
         
            return rowVybaveni;
        }

        private string GetCenovyProfilName()
        {
            foreach (RadioButton rbtn in stpanCenoveProfily.Children)
            {
                if (rbtn.IsChecked == true)
                {
                    return rbtn.Content.ToString();
                }
            }
            return null;
        }
      
        private bool isInputsOK()
        {
            if (!isCorrectTxtKod())
                return false;

            return true;
        }
      
        private bool isCorrectTxtKod()
        {
                if (!Tools.TextBox.TextBoxUpravy.Validate(txtKod, "Kod"))
                {
                    Keyboard.Focus(txtKod);
                    return false;
                }

                //Pro ID s prefixem(typem) vybavy
                if (DataLib.Settings.databases.dbSklad.QueryGetInt("SELECT COUNT(kod) FROM sklad WHERE kod='"+ txtKod.Text+ "'") > 0)
                {
                    System.Windows.MessageBox.Show("Pozor, položka s tímto ID už existuje, proto ji nelze přidat", "Pozor", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Warning);
                    Keyboard.Focus(txtKod);
                    return false;
                }
            return true;
        }

        private void rbtnSpecialni_Checked_1(object sender, RoutedEventArgs e)
        {
         //   txtSpecialni.IsEnabled = true;
        }

        private void rbtnSpecialni_Unchecked_1(object sender, RoutedEventArgs e)
        {
           // txtSpecialni.IsEnabled = false;
        }

        private void txtVelikost_TextChanged(object sender, TextChangedEventArgs e)
        {
            Tools.TextBox.TextBoxUpravy.smazatText(sender, e);
        }

        private void btnPridat_Copy_Click_1(object sender, RoutedEventArgs e)
        {
            CenyProfily cenyProfil = new CenyProfily();
            cenyProfil.ChangedRows += cenyProfil_ChangedRows;
            cenyProfil.Show();
        }

        void cenyProfil_ChangedRows(object sender)
        {
            LoadCenoveProfily();
        }

        private void UserControl_Loaded_1(object sender, RoutedEventArgs e)
        {
            CenovyProfil profil = CenovyProfil.GetCenovyProfilByPrefix("SN");
            LoadCenoveProfily();
        }

        public void LoadCenoveProfily()
        {
            stpanCenoveProfily.Children.Clear();
            List<string> cenoveProfily = DataLib.Settings.databases.dbCeny.QueryGetListString("SELECT Name FROM Ceny");
            foreach (string profil in cenoveProfily)
            {
                RadioButton rbtnProfil = new RadioButton();
             
                rbtnProfil.Margin = new Thickness(5, 5, 0, 0);
                rbtnProfil.Content = profil;

                stpanCenoveProfily.Children.Add(rbtnProfil);
            }
            if (stpanCenoveProfily.Children.Count != 0)
                ((RadioButton)stpanCenoveProfily.Children[0]).IsChecked = true;
        }

        private void txtNazev_TextChanged_1(object sender, TextChangedEventArgs e)
        {

        }
    }
}
